{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Achari Berrada Youssef\n",
    "\n",
    "# This is the script for training the Image Specific Deep Boltzmann Machine\n",
    "import os\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "import config\n",
    "\n",
    "import dbm\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Load all the .npy files in the flickr unlabeled images directory into flickr_u\n",
    "\n",
    "flickr_u = np.array([])\n",
    "files_count = 0\n",
    "\n",
    "for f in os.listdir(config.flickr_unlabeled_path):\n",
    "    if files_count == 10:  # only ten files for now\n",
    "        break\n",
    "    files_count += 1\n",
    "    if f[-4:] == \".npy\":\n",
    "        t = np.load(os.path.join(config.flickr_unlabeled_path, f))\n",
    "        if flickr_u.shape == (0,):\n",
    "            flickr_u = t\n",
    "        else:\n",
    "            flickr_u = np.concatenate((flickr_u, t), axis=0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model = dbm.DBM(\n",
    "    main_dir=\"flickr_rbm\", do_pretrain=True, layers=[1024, 1024],\n",
    "    models_dir=config.models_dir, data_dir=config.data_dir, summary_dir=config.summary_dir,\n",
    "    learning_rate=[0.001, 0.001], momentum=0.9, num_epochs=[1, 1], batch_size=[64, 64],\n",
    "    stddev=0.1, verbose=1, gibbs_k=[1, 1], model_name=\"flickr_dbm\",\n",
    "    finetune_learning_rate=0.01, finetune_enc_act_func=[tf.nn.sigmoid, tf.nn.sigmoid],\n",
    "    finetune_dec_act_func=[tf.nn.sigmoid, tf.nn.sigmoid], finetune_num_epochs=5, finetune_batch_size=128,\n",
    "    finetune_opt='momentum', finetune_loss_func=\"mean_squared\", finetune_dropout=0.5, noise=[\"gauss\", \"bin\"],\n",
    ")\n",
    "\n",
    "trainX = flickr_u[:3000]\n",
    "testX = flickr_u[3000:3500]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#Pretrain the model \n",
    "model.pretrain(trainX, testX)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Fine tunning of the model \n",
    "model.fit(trainX, testX, trainX[:10], testX[:10], save_dbm_image_params=True) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Output : \n",
    "    # image_dbm_layer_1_W.npy\n",
    "    # image_dbm_layer_1_b.npy\n",
    "    # image_dbm_layer_2_W.npy\n",
    "    # image_dbm_layer_2_b.npy"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda env:py27]",
   "language": "python",
   "name": "conda-env-py27-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
